package com.hqd.ch03.v23;


import com.hqd.ch03.v23.listener.BeanFactoryListener;
import com.hqd.ch03.v23.listener.event.BeanFactoryEndEvent;
import com.hqd.ch03.v23.listener.event.BeanFactoryStartedEvent;
import com.hqd.ch03.v23.reader.support.XmlBeanDefinitionReader;
import org.apache.commons.collections.CollectionUtils;

import java.util.Arrays;
import java.util.List;

public class SpringImitationV23Xml extends SpringImitationV23 {
    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);

    public SpringImitationV23Xml(String location) {
        this.reader.loadBeanDefinition(resourceLoader.getResource(location));
        registerListeners();
        publishEvent(new BeanFactoryStartedEvent(this));
        //doSomething
        publishEvent(new BeanFactoryEndEvent(this));
    }

    protected void registerListeners() {
        List<String> listenerBeanNames = Arrays.asList(getBeanNamesForType(BeanFactoryListener.class));
        if (CollectionUtils.isNotEmpty(listenerBeanNames)) {
            for (String listenerBeanName : listenerBeanNames) {
                addBeanFactoryListener(getBean(listenerBeanName, BeanFactoryListener.class));
            }
        }
    }


}
